package de.msiggi.beans;

import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.xml.rpc.ServiceException;

import de.msiggi.Sportsdata.Tools.GameDay;
import de.msiggi.Sportsdata.Tools.GenerateGameDays;
import de.msiggi.Sportsdata.Webservices.League;
import de.msiggi.Sportsdata.Webservices.Matchdata;
import de.msiggi.Sportsdata.Webservices.Sport;
import de.msiggi.Sportsdata.Webservices.SportsdataLocator;
import de.msiggi.Sportsdata.Webservices.SportsdataSoap;

public class ChooseBean {

	/* Sport */

	private List<Sport> sportArt; // all different sports
	private String choosenSportName; // manual choosen sport

	private Sport choosenSport; // automatic set sport
	private List<String> sportName; // ???
	private List<League> leagues; // all different leagues
	private String choosenLeagueName; // manual choosen league

	private League choosenLeague; // automatic set league
	private List<String> season;
	private Matchdata[] matches;
	private List<GameDay> gameDays;

	private SportsdataSoap data;

	public String getChoosenSportName() {
		return choosenSportName;
	}

	public void setChoosenSportName(String choosenSportName) {
		System.out.println("METHODE: setChoosenSportName");
		this.choosenSportName = choosenSportName;
		setChoosenSportByName();
	}

	public String getChoosenLeagueName() {
		return choosenLeagueName;
	}

	public void setChoosenLeagueName(String choosenLeagueName) {
		this.choosenLeagueName = choosenLeagueName;
		setChoosenLeagueByName();
	}

	public ChooseBean() {
		System.out.println("NEW CHOOSEBEAN");
		try {
			data = new SportsdataLocator().getSportsdataSoap();
		} catch (ServiceException e) {
			e.printStackTrace();
		}
	}

	private void setChoosenSportByName() {
		for (Sport sport : sportArt) {
			if (sport.getSportsName().equals(choosenSportName)) {
				choosenSport = sport;
			}
		}
	}

	private void setChoosenLeagueByName() {
		for (League singLeague : leagues) {
			if (singLeague.getLeagueName().equals(choosenLeagueName)) {
				choosenLeague = singLeague;
				try {
					matches = data.getMatchdataByLeagueSaison(
							choosenLeague.getLeagueShortcut(),
							choosenLeague.getLeagueSaison());
				} catch (RemoteException e) {
					e.printStackTrace();
				}
				setMatches(matches);
			}
		}
	}

	public List<Sport> getSport() {
		try {
			sportArt = Arrays.asList(data.getAvailSports());
		} catch (RemoteException e) {
			e.printStackTrace();
		}
		return sportArt;
	}

	public void setSport(List<Sport> sport) {
		this.sportArt = sport;
	}

	public List<League> getLeagues() {
		try {
			leagues = Arrays.asList(data.getAvailLeaguesBySports(choosenSport
					.getSportsID()));
		} catch (RemoteException e) {
			e.printStackTrace();
		}
		return leagues;

	}

	public String getSeason(League league) {
		return league.getLeagueSaison();
	}

	public void setLeagues(List<League> league) {
		this.leagues = league;
	}

	public List<String> getSeason() {
		return season;
	}

	public void setSeason(List<String> season) {
		this.season = season;
	}

	public List<String> getSportName() {
		sportName = new ArrayList<String>();
		if (sportArt == null) {
			getSport();
		}

		for (Sport sport : sportArt) {
			sportName.add(sport.getSportsName());
		}
		return sportName;
	}

	public void setSportName(List<String> sportName) {
		// setChoosenSportByName();
		this.sportName = sportName;
	}

	public Matchdata[] getMatches() {
		Matchdata[] matchdata = null;
		try {
			matchdata = data.getMatchdataByLeagueSaison(
					choosenLeague.getLeagueShortcut(),
					choosenLeague.getLeagueSaison());
		} catch (RemoteException e) {
			e.printStackTrace();
		}

		matches = matchdata;
		return matches;
	}

	public void setMatches(Matchdata[] matches) {
		this.matches = matches;
	}

	public Sport getChoosenSport() {
		return choosenSport;
	}

	public void setChoosenSport(Sport choosenSport) {
		setChoosenSportByName();
		this.choosenSport = choosenSport;
	}

	public List<GameDay> getGameDays() {
		GenerateGameDays gameDayGenerator = new GenerateGameDays();
		gameDays = gameDayGenerator.generateGameDays(matches);
		return gameDays;
	}

	public void setGameDays(List<GameDay> gameDays) {
		this.gameDays = gameDays;
	}
}
